
#include "CxMachine.h"
#include "CxTransaction.h"
#include "CxGlobalVariable.h"

using namespace MSG_SIGN;

void		CxSetTimeCommand::exec(CxMachine *machine)
{
	CxGlobalVariable  gl;

	short   y=(m_data[1] >>4)*10+(m_data[1] & 0x0F)+2000;
	short   m=(m_data[2] >>4)*10+(m_data[2] & 0x0F);
	short   d=(m_data[3] >>4)*10+(m_data[3] & 0x0F);

	short   _h=(m_data[4] >>4)*10+(m_data[4] & 0x0F);
	short   _m=(m_data[5] >>4)*10+(m_data[5] & 0x0F);
	short   _s=(m_data[6] >>4)*10+(m_data[6] & 0x0F);
	
	gl.getTime()->set(boost::posix_time::ptime(boost::gregorian::date(y,m,d),
					boost::posix_time::hours(_h)+boost::posix_time::minutes(_m)+boost::posix_time::seconds(_s))
					);
	sendStdCmd(m_data);	
}

void		CxRdTimeCommand::exec(CxMachine *machine)
{
}

void		CxMarkImgCommand::exec(CxMachine *machine)
{
	sendStdCmd(m_data);
}

void		CxPicCommand::exec(CxMachine  *machine)
{
	int			pic_which=m_data[1];
	int			pic_line=m_data[2];

	_Data		data(m_data.begin()+3,m_data.end());

	data.erase(data.end()-2,data.end());

	machine->findCard(m_cardid)->revpartImage(pic_which,pic_line,data);

	sendStdCmd(m_data);	
}


void	CxProListCommand::exec(CxMachine *machine)
{
	showMode	mode;
	
	mode.m_showmodeIn	=		(m_data[2]>>4) & 0x0F;
	mode.m_showmodeOut	=		m_data[2] & 0x0F;
	mode.b_ispicSeq		=		m_data[3] >> 7 ;
	mode.m_showSpeed	=		(m_data[3] & 0x30) >> 4;
	mode.m_timeshowPos	=		(m_data[3] & 0x0C) >> 2;
	mode.m_timeshowMode	=		m_data[3] & 0x03;
	mode.b_staytimeUnit =		m_data[4] >> 7;
	mode.m_staytime		=		m_data[5];

	machine->findCard(m_cardid)->setShowMode(mode);
	sendStdCmd(m_data);	
}

void		CxShowTextCommand::exec(CxMachine *machine)
{

}

void		CxLineCommand::exec(CxMachine *machine)
{

}

void		CxLoseIsCommand::exec(CxMachine *machine)
{

}

void		CxReadLoseCommand::exec(CxMachine *machine)
{

}

void		CxReadPowerCommand::exec(CxMachine *machine)
{

}

void		CxReadStateCommand::exec(CxMachine *machine)
{	
	CxGlobalVariable  gl;
	_Data *d=new _Data;
	machine->findCard(m_cardid)->rdworkState(*d);
	gl.getPort()->async_write(d->data(),d->size(),[d](bool){delete d;});
}


void		CxCommonCommand::exec(CxMachine *machine)
{
	_Data	*d;
	CxGlobalVariable  gl;

	switch(id)
	{
	case 'Q':
		settmpOption(machine);
		sendStdCmd(m_data);
		break;
	case 'S':
		setrelyOption(machine);
		sendStdCmd(m_data);
		break;
	case 'P':
		machine->findCard(m_cardid)->setLight(m_data[1]);
		break;
	default:
		sendStdCmd(m_data);
		break;
	}
}


void		CxCommonCommand::settmpOption(CxMachine *machine)
{
	tmpOption  option;


	option.m_tmppowerMax=m_data[1];
	option.m_tmppowerMin=m_data[2];
	option.m_tmpfanMax=m_data[4];
	option.m_tmpfanMin=m_data[5];

	option.b_isautoOff=(m_data[6] & 0x08)>>3;
	option.b_isautoFan=(m_data[6] & 0x04)>>2;
	option.b_isautoPower=(m_data[6] & 0x02) >> 1;
	option.b_issmokeautoOff=m_data[6] & 0x01;

	machine->findCard(m_cardid)->settmpOption(option);
}


void		CxCommonCommand::setrelyOption(CxMachine *machine)
{
	machine->findCard(m_cardid)->setRely(m_data[1],m_data[2]);
}